//
//  DXWAllChannelCell.m
//  pandaTV
//
//  Created by 淡馨慰 on 16/11/2.
//  Copyright © 2016年 淡馨慰. All rights reserved.
//

#import "DXWAllChannelCell.h"
#import "DXWChannelCell.h"
#import "DXWChannelItem.h"
#import "DXWChannelGroupItem.h"


@interface DXWAllChannelCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

/** 存储模型数据 */
//@property (nonatomic, strong) NSMutableArray *channel;

@property (nonatomic, assign) CGFloat itemH;

@end

static NSString *ID = @"channelCell";

@implementation DXWAllChannelCell

+ (instancetype)allChannelCell
{
    return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil]lastObject];
}

//- (void)setGroupItem:(DXWChannelGroupItem *)groupItem
//{
//    _groupItem = groupItem;
//    _channel = [NSMutableArray arrayWithArray:groupItem.channel];
//    NSLog(@"%@",_channel);
//}


- (void)setChannels:(NSMutableArray *)channels
{
    _channels = [[NSMutableArray alloc]initWithArray:channels];
    NSLog(@"%@",_channels);
    [self.collectionView reloadData];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    //设置layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    NSInteger col = 3;
    layout.itemSize = CGSizeMake(tagW, tagH);
    layout.minimumLineSpacing = dxw_margin_10;
    layout.minimumInteritemSpacing = dxw_margin_10;
    layout.sectionInset = UIEdgeInsetsMake(dxw_margin_10, dxw_margin_10, dxw_margin_10, dxw_margin_10);
    
    //设置collectionView
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.contentView.bounds collectionViewLayout:layout];
    
    collectionView.dataSource = self;
//    collectionView.delegate = self;
    self.collectionView = collectionView;
    
    [self.contentView addSubview:collectionView];
    
    [collectionView registerNib:[UINib nibWithNibName:@"DXWChannelCell" bundle:nil] forCellWithReuseIdentifier:ID];
//    [collectionView setScrollEnabled:NO];
    
}
#pragma mark -UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.channels.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DXWChannelCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    DXWChannelItem *item = self.channels[indexPath.row];
    cell.item = item;
    return cell;
}

#pragma mark -cell的btn的点击
- (void)btnClick:(NSIndexPath *)indexPath
{
    [self collectionView:_collectionView didSelectItemAtIndexPath:indexPath ];

//    DXWChannelItem *item = self.channels[indexPath.row];
//      NSLog(@"%ld",indexPath.row);
    NSLog(@"%@",indexPath);
}

#pragma mark -UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    NSLog(@"%ld",indexPath.row);
}

- (CGFloat)heightOfCell
{
    CGFloat originY = 27;
    
    NSInteger cols = 3;
    NSInteger rows = (self.channels.count - 1) / cols + 1;
    return rows * (tagH + dxw_margin_10) + originY;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


@end
